package com.qianxx.viewlibrary.effects.scrollpanel;

import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

public class ScrollUtils {
    //ScrollPanel是否拦截焦点事件
    static boolean isIntercept = true;

    /**
     * 设置ScrollPanel，监听并设置ScrollPanel是否拦截事件
     *
     * @param scrollPanel
     */
    public void setScrollPanel(final ScrollPanel scrollPanel) {
        if (scrollPanel != null)
            scrollPanel.setOnInterceptTouchListener(new ScrollPanel.OnInterceptTouchListener() {

                @Override
                public boolean onScrollToTop() {
                    return isIntercept;
                }
            });
    }

    /**
     * 设置垂直RecycleView，监听RecycleView的垂直滚动事件，
     * 判断RecycleView是否滚动到顶部，进而设置ScrollPanel是否要拦截事件
     *
     * @param recycleViews
     */
    public void setVerticalRecycleView(RecyclerView... recycleViews) {
        if (recycleViews != null) {
            for (RecyclerView recyclerView : recycleViews)
                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        int curPos = -1;
                        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                            curPos = layoutManager.findFirstVisibleItemPosition();
                        } else if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                            GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                            curPos = layoutManager.findFirstVisibleItemPosition();
                        }
                        isIntercept = curPos == 0 ? true : false;
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);

                    }
                });
        }
    }

    /**
     * 设置水平RecycleView，监听RecycleView的点击事件，
     * 进而设置ScrollPanel是否要拦截事件
     *
     * @param recycleViews
     */
    public void setHorizontalRecyclerView(RecyclerView... recycleViews) {
        if (recycleViews != null)
            for (RecyclerView recyclerView : recycleViews)
                recyclerView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                isIntercept = true;
                                break;
                            case MotionEvent.ACTION_UP:
                                isIntercept = false;
                                break;
                        }
                        return false;
                    }
                });
    }

    /**
     * 设置水平AppBarLayout，监听AppBarLayout的滚动事件，
     * 判断AppBarLayout是否滚动到顶部，进而设置ScrollPanel是否要拦截事件
     *
     * @param appbar
     */
    public void setAppBarLayout(AppBarLayout appbar) {
        if (appbar != null)
            appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    isIntercept = verticalOffset == 0 ? true : false;
                }
            });
    }
}
